package com.peanut.baby.mvp.main;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.PTag;
import com.peanut.baby.model.ShareType;
import com.peanut.baby.model.User;
import com.peanut.baby.model.VarPoint;
import com.tencent.mars.xlog.Log;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/8/22.
 */

public class ProfileFragmentPresenter implements ProfileFragmentContract.Presenter{
    private static final String TAG = ProfileFragment.class.getSimpleName();
    ProfileFragmentContract.View view;
    Activity activity;
    public ProfileFragmentPresenter(ProfileFragmentContract.View view,Activity activity) {
        this.view = view;
        this.activity = activity;
    }

    @Override
    public void getSelfInfo() {
        if(InitManager.getInstance().getUser() == null){
            view.onGetInfoFailed("用户未登录");
            return;
        }
        Observable obs = RetrofitClient.getInstance().getMyInfo(InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<User>compose()).compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<User>() {
            @Override
            protected void onHandleSuccess(User o) {
                view.OnSelfInfoGet(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onGetInfoFailed(msg);
            }
        });
    }

    @Override
    public void sharePoint() {
        Observable obs = RetrofitClient.getInstance().sharePoint(InitManager.getInstance().getUserId(),
                ShareType.TYPE_SHARE_APP+"","");
        obs.compose(BaseScheduler.<VarPoint>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<VarPoint>() {
            @Override
            protected void onHandleSuccess(VarPoint o) {
                InitManager.getInstance().getUser().userPoint = o.totalPoint;
                InitManager.getInstance().updateLocalUser();
                view.onSharePointSuccess("花生壳加"+o.variedPoint+",总花生壳"+o.totalPoint);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                Log.w(TAG,"sharePoint failed "+msg);
//                view.onSharePointSuccess(msg);
            }
        });
    }

    @Override
    public void getSubscribedTags() {
        Observable obs = RetrofitClient.getInstance().getSubscribedTagList(InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<List<PTag>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<List<PTag>>() {
                    @Override
                    protected void onHandleSuccess(List<PTag> o) {
                        if(o != null && !o.isEmpty()){
                            InitManager.getInstance().setTags(o);
                        }
                        view.onSubscribedTagsGet(true,o,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        Log.e(TAG,"failed to get subscribed tags: "+msg);
                        view.onSubscribedTagsGet(false,null,msg);
                    }
                });
    }
}
